package com.android.infantschool.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.nfc.Tag;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.infantschool.R;
import com.android.infantschool.bean.DynamicBean;
import com.android.infantschool.bean.ReleaseDynamicBean;
import com.android.infantschool.util.ImageUtil;
import com.bumptech.glide.Glide;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class ReleaseDynamicAdapter extends RecyclerView.Adapter<ReleaseDynamicAdapter.MyHolder> {
    private Context mContext;
    public List<ReleaseDynamicBean> list = null;
    private OnImgLister lister;

    public ReleaseDynamicAdapter(Context context, OnImgLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_release_dynamic, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        final ReleaseDynamicBean bean = list.get(position);
        if (bean.type == 1) {//图片
            viewHolder.imgPlay.setVisibility(View.GONE);
        }

        if (bean.type == 2) {//视频
            viewHolder.imgPlay.setVisibility(View.VISIBLE);
        }

        int size = getItemCount() - 1;
        if (position == size) {
            Log.e(getClass().getSimpleName(), "删除当前条目:" + position);
            viewHolder.imgDelete.setVisibility(View.GONE);
        } else {
            viewHolder.imgDelete.setVisibility(View.VISIBLE);
        }

        Glide.with(mContext)
                .load(bean.path)
                .into(viewHolder.imgView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(getClass().getSimpleName(), "点中了:" + position);
                if (lister != null) {
                    lister.imgLister(position, bean.type);
                }
            }
        });

        viewHolder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lister != null) {
                    lister.deleteLister(position, bean.type);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<ReleaseDynamicBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param bean
     */
    public void addData(ReleaseDynamicBean bean) {
        if (getItemCount() > 10) {
            return;
        }
        this.list.add(0, bean);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addAllData(List<ReleaseDynamicBean> list) {
        if (getItemCount() > 10) {
            return;
        }
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 删除某一项
     *
     * @param position
     */
    public void deleteItem(int position) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.remove(position);
            }
        }
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgView;
        public ImageView imgDelete;
        public ImageView imgPlay;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgView = itemView.findViewById(R.id.img_item);
            imgDelete = itemView.findViewById(R.id.img_delete);
            imgPlay = itemView.findViewById(R.id.img_play);
        }
    }

    public interface OnImgLister {
        void imgLister(int position, int type);

        void deleteLister(int position, int type);
    }
}